﻿using Elasticsearch.Net;
using Microsoft.Extensions.Configuration;
using Nest;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Ldt.CaseRetrievalApp.ESlibrary
{
    /// <summary>
    /// Es数据检索服务端
    /// </summary>
    public class ESSever : IESSever
    {
        /// <summary>
        /// Linq查询的官方Client
        /// </summary>
        public IElasticClient ElasticLinqClient { get; set; }
        /// <summary>
        /// Json查询的官方Client
        /// </summary>
        public IElasticLowLevelClient ElasticJsonClient { get; set; }
        /// <summary>
        /// 传入es地址、进行数据的检索
        /// </summary>
        /// <param name="url"></param>
        public ESSever(string url)
        {
            var settings = new ConnectionSettings(new Uri(url));
            this.ElasticJsonClient = new ElasticLowLevelClient(settings);//json请求客户端初始化
            this.ElasticLinqClient = new ElasticClient(settings);//linq请求客户端初始化
        }
        /// <summary>
        /// 实例化
        /// </summary>
        /// <param name="configuration"></param>
        public ESSever(IConfiguration configuration)
        {
            var uris = configuration["ElasticSearchContext:Url"].Split(",").ToList().ConvertAll(x => new Uri(x));//配置节点地址，以，分开
            //var uris = new List<Uri> { new Uri("http://192.168.0.80:9200") };
            var connectionPool = new StaticConnectionPool(uris);//配置请求池
            var settings = new ConnectionSettings(connectionPool).BasicAuthentication("kibana", "kibana").RequestTimeout(TimeSpan.FromSeconds(30));//请求配置参数
            this.ElasticJsonClient = new ElasticLowLevelClient(settings);//json请求客户端初始化
            this.ElasticLinqClient = new ElasticClient(settings);//linq请求客户端初始化
        }


    }
}
